#include "Turret.h"

Turret::Turret(): mShootTimer(0), mTargetRotation(0), mDistance(0,0,0), mCanFire(true), mAssignedMarker(0)
{

}

Turret::~Turret()
{

}
Gun* Turret::GetGun()
{
	return &mGun;
	
}

float Turret::GetRotationZ()
{
	return mTargetRotation;
}

bool Turret::CanFire()
{
		return mCanFire;
}
int Turret::GetAssignedMarker()
{
	return mAssignedMarker;
}

//-- Assign a marker to the turret
void Turret::SetAssignedMarker(int marker)
{
	mAssignedMarker = marker;
}
//-- Update the turret
void Turret::Update(abfw::Matrix44 marker, abfw::Matrix44 enemyOrigin, std::vector<Enemy> theEnemies )
{
	//-- Check there is at least 1 enemy on screen
	if (theEnemies.size() > 0)
	{
		//-- Gets the distance to the first enemy
		abfw::Matrix44 targetMat = *theEnemies[0].GetLocalTransform() * enemyOrigin;
		abfw::Vector3 closestDistance = targetMat.GetTranslation() - marker.GetTranslation();

		//-- Will store the closest enemy 
		int closestEnemy = 0;
		
		//-- For each enemy (minus the first enemy which has already been accounted for)
		for (int i = 1; i < theEnemies.size(); i++)
		{
			//-- The enemy location
			targetMat = *theEnemies[i].GetLocalTransform() * enemyOrigin;
			//-- Distance to enemy
			mDistance = targetMat.GetTranslation() - marker.GetTranslation();

			//-- Check if this enemy is closer than the closest
			if ( mDistance.Length() < closestDistance.Length())
			{
				//-- if yes, set the cloest enemy to this index and update closest distance 
				closestEnemy = i;
				closestDistance = mDistance;
			}
		}
	
		//-- Only shoot it if its within range
		if (closestDistance.Length() <= 0.15)
			{
				//-- Calculate the target rotation
				mCanFire = true;
				targetMat = *theEnemies[closestEnemy].GetLocalTransform() * enemyOrigin;
				abfw::Vector3 target = targetMat.GetTranslation();
				abfw::Vector3 currentPosition = marker.GetTranslation();

				mTargetRotation = (float)atan2(target.y - currentPosition.y, target.x - currentPosition.x);

			}
			else
			{
				//-- Can't see enemy so just rotate on the spot
				mTargetRotation+=0.005;
				mCanFire = false;
			}

	}
	else
	{
		//-- No enemies so just rotate on the spot
		mTargetRotation+=0.005;
		mCanFire = false;
	}
	
	//-- Set transformation matrices
	SetLocalScale(abfw::Vector3(0.002195f, 0.002195f, 0.002195f));
	SetLocalRotationX(1.6f);	
	SetLocalRotationY(0.0f);
	SetLocalRotationZ(mTargetRotation-3.2f);

	marker.SetTranslation(abfw::Vector3(marker.GetTranslation().x,
											marker.GetTranslation().y,
												marker.GetTranslation().z+0.01));
	//-- Set final transform
	SetTransform(GetLocalScale() * GetLocalRotation() * marker);
}

//-- Fire the turrets gun
void Turret::Fire(abfw::Mesh* mesh, abfw::Matrix44 marker)
{
	mGun.Fire(GetRotationZ(), mesh ,marker);
}

void Turret::Draw(abfw::Renderer3D* render3D)
{
	//-- Draw turret
	render3D->DrawMesh(GetMeshInstance());

}

void Turret::DrawBullets(abfw::Renderer3D* render3D)
{
	//-- Draw turret bullets
	mGun.DrawBullets(render3D);
}

